package com.itheima.service.Impl;

import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Map;

public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    @Override
    public Result order(Map map) throws Exception {
        //检查用于预约这一天我们是不是进行过预约设置,如果设置过,才预约成功,否则不能进行预约
        String orderDate = (String) map.get("orderDate");
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(DateUtils.parseString2Date(orderDate));
        if (orderSetting == null) {
            //表示就是这一天我们还没有进行过预约设置,所以就不能就行预约
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //检查预约人数有没有约满
        //number是我们今天一共设置了多少人可以预约
        int number = orderSetting.getNumber();
        //reservation是我们今天已经预约了多少人了
        int reservations = orderSetting.getReservations();
        if (reservations >= number) {//表示已经约满了
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        //检查用户有没有重复预约,就是说一天之内有没有预约同一个套餐两次,先根据电话,查到对应的这个人
        String telephone = (String) map.get("telephone");
        Member member = memberDao.findByTelephone(telephone);
        //如果确实是有这个人,再判断他有没有重复预约
        if(member!=null){
            Integer id = member.getId();//会员的id
            Date s = DateUtils.parseString2Date(orderDate);//会员的预约日期
            String setmealId = (String) map.get("setmealId");//预约的套餐id
            //就把这个人的信息根据这三个情况到预约表里面
            Order order=new Order(id,s,Integer.parseInt(setmealId));
            List<Order> list = orderDao.findByCondition(order);
            if(list != null && list.size() > 0) {
                //说明用户在重复预约，无法完成再次预约
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
        }else{
            //他还不是会员,我们就自动给他注册
            Member member1 = new Member();
            String telephone1 = (String) map.get("telephone");
            member1.setPhoneNumber(telephone1);
            member.setName((String) map.get("name"));
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            memberDao.add(member);//自动完成会员注册
        }
        //所有验证都通过了,所以我们就可以把在这个预约人的信息保存到预约表格,也就是order表格
        Order order = new Order();
        order.setMemberId(member.getId());//设置会员ID
        order.setOrderDate(DateUtils.parseString2Date(orderDate));//预约日期
        order.setOrderType((String) map.get("orderType"));//预约类型
        order.setOrderStatus(Order.ORDERSTATUS_NO);//到诊状态
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));//套餐ID
        orderDao.add(order);

        //更新当日的预约人数 设置已预约人数+1
        orderSetting.setReservations(orderSetting.getReservations()+1);
        orderSettingDao.editReservationsByOrderDate(orderSetting);
        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }

    @Override
    public Map findById(Integer id) {
        Map map = orderDao.findById4Detail(id);
        if(map != null){
            //处理日期格式
            Date orderDate = (Date) map.get("orderDate");
            try {
                map.put("orderDate",DateUtils.parseDate2String(orderDate));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return map;
    }
}
